
#include "FrogDao.h"


FrogDao::FrogDao(void) {
	this->db = DB::getInstance();
}	

FrogDao::~FrogDao(void){

}

void FrogDao::save(Element * object){
	try {
		if(object->getId().empty()) {
			object->setId(this->db->getSequenceID());
		}
		this->find(object->getId());
		throw PersistenceUniqueConstraintException("Se intento guardar un elemento duplicado.");
	}catch(NoSuchElementException e) {
		this->db->getFrogs()->insert(make_pair(object->getId(),object));
	}	
}

Element * FrogDao::find(string id){
	map<string,Element *>::iterator iter = db->getFrogs()->find(id);
	if(iter != db->getFrogs()->end()) {
		return iter->second;
	}
	throw NoSuchElementException("No existe el elemento con ese id");
}

list<Element *> * FrogDao::findAll(void){
	list<Element *> * result = new list<Element *>();
	map<string,Element *>::iterator iter;
	for(iter = this->db->getFrogs()->begin() ; iter != this->db->getFrogs()->end() ; ++iter) {
		result->push_front(iter->second);
	}
	return result;
}

void FrogDao::deleteObject(Element * objectToDelete){

}

